﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager1 : MonoBehaviour
{
    public int m_NumRoundsToWin = 5;
    public float m_StartDelay = 3f;
    public float m_EndDelay = 3f;
    public CameraControl1 m_CameraControl1;
    public Text m_MessageText;
    public GameObject m_TankPrefab;
    public TankManager1[] m_Tanks;

    private int m_RoundNum;
    private TankManager1 m_RoundWinTank;
    private TankManager1 m_GameWinTank;
    private WaitForSeconds m_StartWait;
    private WaitForSeconds m_EndWait;

    private void Start()
    {
        m_StartWait = new WaitForSeconds(m_StartDelay);
        m_EndWait = new WaitForSeconds(m_EndDelay);

        SpawnAllTanks();
        SetCameraTargets();

        StartCoroutine(GameLoop());
    }

    void SpawnAllTanks()
    {
        for(int i = 0; i< m_Tanks.Length; i++)
        {
            m_Tanks[i].m_Instance = Instantiate(m_TankPrefab, m_Tanks[i].m_TransformPoint.position,m_Tanks[i].m_TransformPoint.rotation) as GameObject;
            m_Tanks[i].m_PlayerNum = i + 1;
            m_Tanks[i].SetUp();
        }
    }

    void SetCameraTargets()
    {
        Transform[] target = new Transform[m_Tanks.Length];
        for(int i = 0;i< m_Tanks.Length; i++)
        {
            target[i] = m_Tanks[i].m_Instance.transform;
        }
        m_CameraControl1.m_Targets = target;
    }

    IEnumerator GameLoop()
    {
        Debug.Log("GameLoop");
        yield return StartRound();
        yield  return PlayingRound();
        yield return EndRound();

        if(m_GameWinTank != null)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
        else
        {
            StartCoroutine(GameLoop());
        }

    }

    IEnumerator StartRound()
    {
        Debug.Log("StartRound");
        DisableAllTanks();
        RestAllTanks();
        m_CameraControl1.SetStartPositionAndSize();
        m_MessageText.text = "Round " + m_RoundNum+1;
        m_RoundNum++;
        yield return m_StartWait;
    }

    IEnumerator PlayingRound()
    {
        EnableAllTanks();
        m_MessageText.text = string.Empty;

        while(!OneTanksLeft())
        {
            yield return null;
        }

    }

    IEnumerator EndRound()
    {
        Debug.Log("EndRound");
        DisableAllTanks();
        SetRoundWinner();
        SetGameWinner();
        /*        Debug.Log(m_GameWinTank.m_PlayerNum);
                Debug.Log(m_RoundWinTank.m_PlayerNum);*/

        if (m_RoundWinTank != null)
        {
            m_MessageText.text += "Round Winner is " + m_RoundWinTank.m_ColorPlayerText + "!!\n\n\n\n";
        }
        else
        {
            m_MessageText.text += "Equle!\n\n\n\n";
        }

        for (int i = 0; i < m_Tanks.Length; i++)
        {
            m_MessageText.text += m_Tanks[i].m_ColorPlayerText + ":" + m_Tanks[i].m_wins + "Wins\n";
        }


        yield return m_EndWait;
    }
    void SetRoundWinner()
    {
        m_RoundWinTank = null;
        for (int i = 0; i < m_RoundNum; i++)
        {
            if(m_Tanks[i].m_Instance.activeSelf)
            {
                m_RoundWinTank = m_Tanks[i];
                m_Tanks[i].m_wins++;
            }
        }

    }

    void SetGameWinner()
    {
        m_GameWinTank = null;
        for(int i = 0; i < m_RoundNum; i++)
        {
            if(m_Tanks[i].m_wins == 5)
            {
                m_GameWinTank = m_Tanks[i];
            }
        }

    }

    bool OneTanksLeft()
    {
        int tanksNum = 0;
        for(int i = 0; i < m_Tanks.Length; i++)
        {
            if(m_Tanks[i].m_Instance.activeSelf)
            {
                tanksNum++;
                
            }
        }
        if (tanksNum == 1)
        {
            return true;
        }
        return false;
    }

    void DisableAllTanks()
    {
        for(int i = 0;i < m_Tanks.Length; i++)
        {
            m_Tanks[i].DisableControl();
        }
    }

    void EnableAllTanks()
    {
        for (int i = 0; i < m_Tanks.Length; i++)
        {
            m_Tanks[i].EnableControl();
        }
    }

    void RestAllTanks()
    {
        for (int i = 0; i < m_Tanks.Length; i++)
        {
            m_Tanks[i].Reset();
        }
    }
}
